package com.antball.aaa.obsever_mediator;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

/**
 * @Auther: huangsj
 * @Date: 2019/8/8 11:07
 * @Description:
 */
public class EventDispatch implements Observer {

    private final static EventDispatch eventDispatch = new EventDispatch();

    private Vector<EventConsumer> eventConsumers = new Vector<EventConsumer>();


    public static EventDispatch getEventDispatch(){
        return eventDispatch;
    }

    @Override
    public void update(Observable o, Object arg) {
        Control control = (Control)arg;

        ControlEvent controlEvent=(ControlEvent)o;

        for (EventConsumer consumer: eventConsumers){
            if(consumer.getEventType() == controlEvent.getEventType()){
                consumer.exec(controlEvent);
            }
        }

    }

    public  void registeConsumer(EventConsumer consumer){

        eventConsumers.add(consumer);

    }
}
